

# Cache

<div class="api-docs__separator" data-reactroot="">

---

</div><div class="api-docs__import" data-reactroot="">

```ts
import { Cache } from "@hyper-fetch/core"
```

</div><div class="api-docs__section">

## Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Cache class handles the data exchange with the dispatchers.

</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:25](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L25)

</p><div class="api-docs__section">

## Parameters

</div><div class="api-docs__parameters"><table><thead><tr><th>Name</th><th>Details</th></tr></thead><tbody><tr param-data="client"><td class="api-docs__param-name required">

### client 

`Required`

</td><td class="api-docs__param-type">

`C`

</td></tr><tr param-data="options"><td class="api-docs__param-name optional">

### options 

`Optional`

</td><td class="api-docs__param-type">

`CacheOptionsType<ClientInstance>`

</td></tr></tbody></table></div><div class="api-docs__section">

## Properties

</div><div class="api-docs__properties"><div class="api-docs__property" property-data="clearKey"><h3 class="api-docs__name">

### `clearKey`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">



</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:31](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L31)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
string
```

</div><hr/></div><div class="api-docs__property" property-data="client"><h3 class="api-docs__name">

### `client`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">



</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:35](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L35)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
C
```

</div><hr/></div><div class="api-docs__property" property-data="emitter"><h3 class="api-docs__name">

### `emitter`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">



</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:26](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L26)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
EventEmitter
```

</div><hr/></div><div class="api-docs__property" property-data="events"><h3 class="api-docs__name">

### `events`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">



</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:27](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L27)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
{ emitCacheData: <Response,Error,Adapter>(cacheKey: string, data: CacheValueType<Response, Error, Adapter>) => void; emitInvalidation: (cacheKey: string) => void; onData: <Response,Error,Adapter>(cacheKey: string, callback: (data: CacheValueType<Response, Error, Adapter>) => void) => VoidFunction; onInvalidate: (cacheKey: string, callback: () => void) => VoidFunction }
```

</div><hr/></div><div class="api-docs__property" property-data="garbageCollectors"><h3 class="api-docs__name">

### `garbageCollectors`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">



</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:32](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L32)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
Map<string, Timeout>
```

</div><hr/></div><div class="api-docs__property" property-data="lazyStorage"><h3 class="api-docs__name">

### `lazyStorage`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">



</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:30](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L30)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
CacheAsyncStorageType
```

</div><hr/></div><div class="api-docs__property" property-data="options"><h3 class="api-docs__name">

### `options`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">



</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:35](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L35)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
CacheOptionsType<ClientInstance>
```

</div><hr/></div><div class="api-docs__property" property-data="storage"><h3 class="api-docs__name">

### `storage`

</h3><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">



</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:29](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L29)

</p><div class="api-docs__section">

#### Type

</div><div class="api-docs__property-type">

```ts
CacheStorageType
```

</div><hr/></div></div><div class="api-docs__section">

## Methods

</div><div class="api-docs__methods"><div class="api-docs__method" method-data="clear"><h3 class="api-docs__name">

### `clear()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
clear()
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Clear cache storages

</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:274](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L274)

</p><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
Promise<void>
```

</div><hr/></div><div class="api-docs__method" method-data="delete"><h3 class="api-docs__name">

### `delete()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
delete(cacheKey)
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Delete record from storages and trigger invalidation event

</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:161](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L161)

</p><div class="api-docs__section">

#### Parameters

</div><div class="api-docs__parameters"><table><thead><tr><th>Name</th><th>Details</th></tr></thead><tbody><tr param-data="cacheKey"><td class="api-docs__param-name required">

#### cacheKey 

`Required`

</td><td class="api-docs__param-type">

`string`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
void
```

</div><hr/></div><div class="api-docs__method" method-data="get"><h3 class="api-docs__name">

### `get()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
get<Response, Error, Adapter>(cacheKey)
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Get particular record from storage by cacheKey. It will trigger lazyStorage to emit lazy load event for reading it&#x27;s data.

</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:139](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L139)

</p><div class="api-docs__section">

#### Parameters

</div><div class="api-docs__parameters"><table><thead><tr><th>Name</th><th>Details</th></tr></thead><tbody><tr param-data="cacheKey"><td class="api-docs__param-name required">

#### cacheKey 

`Required`

</td><td class="api-docs__param-type">

`string`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
{
  data: GenericDataType | null;
  error: GenericErrorType | null;
  extra: T extends AdapterType<any, any, any, infer A, any> ? A : never | null;
  status: T extends AdapterType<any, any, infer S, any, any> ? S : never | null;
  success: boolean;
  isCanceled: boolean;
  isOffline: boolean;
  retries: number;
  timestamp: number;
  cacheTime: number;
  clearKey: string;
  garbageCollection: number;
}
```

</div><hr/></div><div class="api-docs__method" method-data="getLazyKeys"><h3 class="api-docs__name">

### `getLazyKeys()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
getLazyKeys()
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Used to receive keys from sync storage and lazy storage

</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:229](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L229)

</p><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
Promise<string[]>
```

</div><hr/></div><div class="api-docs__method" method-data="getLazyResource"><h3 class="api-docs__name">

### `getLazyResource()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
getLazyResource<Response, Error, Adapter>(cacheKey)
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Used to receive data from lazy storage

</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:194](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L194)

</p><div class="api-docs__section">

#### Parameters

</div><div class="api-docs__parameters"><table><thead><tr><th>Name</th><th>Details</th></tr></thead><tbody><tr param-data="cacheKey"><td class="api-docs__param-name required">

#### cacheKey 

`Required`

</td><td class="api-docs__param-type">

`string`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
Promise<CacheValueType<Response, Error, Adapter>>
```

</div><hr/></div><div class="api-docs__method" method-data="invalidate"><h3 class="api-docs__name">

### `invalidate()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
invalidate(cacheKey)
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Invalidate cache by cacheKey or partial matching with RegExp

</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:172](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L172)

</p><div class="api-docs__section">

#### Parameters

</div><div class="api-docs__parameters"><table><thead><tr><th>Name</th><th>Details</th></tr></thead><tbody><tr param-data="cacheKey"><td class="api-docs__param-name required">

#### cacheKey 

`Required`

</td><td class="api-docs__param-type">

`string | RegExp`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
Promise<void>
```

</div><hr/></div><div class="api-docs__method" method-data="keys"><h3 class="api-docs__name">

### `keys()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
keys()
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Get sync storage keys, lazyStorage keys will not be included

</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:151](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L151)

</p><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
string[]
```

</div><hr/></div><div class="api-docs__method" method-data="scheduleGarbageCollector"><h3 class="api-docs__name">

### `scheduleGarbageCollector()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
scheduleGarbageCollector(cacheKey)
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Schedule garbage collection for given key

</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:242](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L242)

</p><div class="api-docs__section">

#### Parameters

</div><div class="api-docs__parameters"><table><thead><tr><th>Name</th><th>Details</th></tr></thead><tbody><tr param-data="cacheKey"><td class="api-docs__param-name required">

#### cacheKey 

`Required`

</td><td class="api-docs__param-type">

`string`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
Promise<void>
```

</div><hr/></div><div class="api-docs__method" method-data="set"><h3 class="api-docs__name">

### `set()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
set<Request>(request, response)
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Set the cache data to the storage

</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:62](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L62)

</p><div class="api-docs__section">

#### Parameters

</div><div class="api-docs__parameters"><table><thead><tr><th>Name</th><th>Details</th></tr></thead><tbody><tr param-data="request"><td class="api-docs__param-name required">

#### request 

`Required`

</td><td class="api-docs__param-type">

`RequestInstance | RequestJSON<any, unknown, QueryParamsType, null>`

</td></tr><tr param-data="response"><td class="api-docs__param-name required">

#### response 

`Required`

</td><td class="api-docs__param-type">

`CacheMethodType<ResponseReturnType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>> & ResponseDetailsType>`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
void
```

</div><hr/></div><div class="api-docs__method" method-data="update"><h3 class="api-docs__name">

### `update()`

</h3><div class="api-docs__section">

#### Preview

</div><div class="api-docs__preview fn">

```ts
update<Request>(request, partialResponse)
```

</div><div class="api-docs__section">

#### Description

</div><div class="api-docs__description"><span class="api-docs__do-not-parse">

Update the cache data with partial response data

</span></div><p class="api-docs__definition">

Defined in [cache/cache.ts:111](https://github.com/BetterTyped/hyper-fetch/blob/cc329b91/packages/core/src/cache/cache.ts#L111)

</p><div class="api-docs__section">

#### Parameters

</div><div class="api-docs__parameters"><table><thead><tr><th>Name</th><th>Details</th></tr></thead><tbody><tr param-data="request"><td class="api-docs__param-name required">

#### request 

`Required`

</td><td class="api-docs__param-type">

`RequestInstance | RequestJSON<RequestInstance, unknown, QueryParamsType, null>`

</td></tr><tr param-data="partialResponse"><td class="api-docs__param-name required">

#### partialResponse 

`Required`

</td><td class="api-docs__param-type">

`CacheMethodType<Partial<ResponseReturnType<ExtractResponseType<Request>, ExtractErrorType<Request>, ExtractAdapterType<Request>> & ResponseDetailsType>>`

</td></tr></tbody></table></div><div class="api-docs__section">

#### Return

</div><div class="api-docs__returns">

```ts
void
```

</div><hr/></div></div>